﻿
(function () {
    appModule.controller('common.views.disputeInformations.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.disputeInformation', 'disputeInformationId', 'abp.services.app.disputeType', 'disputeInformation', 'abp.services.app.area', 'abp.services.app.user', 'textAngularManager',
        function ($scope, $uibModalInstance, disputeInformationService, disputeInformationId, disputeTypeService, disputeInformation, areaService, userService, textAngularManager) {
            var vm = this;
            vm.saving = false;
            //首先将disputeInformation数据设置为null
            vm.disputeInformation = null;



            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                if (disputeInformationId != null) {
                    disputeInformationService.createOrUpdateDisputeInformationCommonAsync({ disputeInformationEditDto: vm.disputeInformation }).then(function () {
                        abp.notify.info(app.localize('SavedSuccessfully'));
                        $uibModalInstance.close();
                    }).finally(function () {
                        vm.saving = false;
                    });
                }
                else {
                    disputeInformationService.createOrUpdateDisputeInformationAsync({ disputeInformationEditDto: vm.disputeInformation }).then(function () {
                        abp.notify.info(app.localize('SavedSuccessfully'));
                        $uibModalInstance.close();
                    }).finally(function () {
                        vm.saving = false;
                    });
                }
            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //上传人选中事件
            vm.userIDChange = function (userID) {
                for (var i = 0; i < vm.userList.length; i++) {
                    if (vm.userList[i].id == userID) {
                        vm.disputeInformation.uploadName = vm.userList[i].name;
                    }
                }
            };

            //初始化页面
            function init() {
                //绑定纠纷来源
                disputeInformationService.getAllDisSourceList().then(function (result) {
                    vm.disSourceList = result.data;
                });

                //绑定紧急程度
                disputeInformationService.getAllDisUrgencyList().then(function (result) {
                    vm.disUrgencyList = result.data;
                });

                //绑定纠纷分类
                disputeTypeService.getAllDisputeTypeList().then(function (result) {
                    vm.disputeTypeList = result.data.items;
                });

                //绑定用户控件
                userService.getAllUsersByWhere({}).then(function (result) {
                    vm.userList = result.data.items;
                    var b = disputeInformationId != null;

                    var townId = '';
                    var areaId = '';
                    var currUser;

                    //绑定数据
                    disputeInformationService.getDisputeInformationForEditAsync({
                        id: disputeInformationId
                    }).then(function (result) {
                        if (b) {
                            currUser = result.data.disputeInformation;
                            townId = disputeInformation.townID;
                            areaId = disputeInformation.areaID;

                            InitAreas();
                            vm.disputeInformation = result.data.disputeInformation;
                            vm.disputeInformation.userID = vm.disputeInformation.userID + "";
                        } else {
                            vm.disputeInformation = result.data.disputeInformation;
                            //绑定默认值
                            vm.disputeInformation.sourceID = 1;
                            vm.disputeInformation.disputeTypeID = 1;
                            vm.disputeInformation.urgencyID = 1;

                            //获取Session中的userId
                            var userid = abp.session.userId;
                            //获取用户数据
                            userService.getUsersGyUserId({ userid: userid }).then(function (result) {
                                currUser = result.data;
                                townId = currUser.townID;
                                areaId = currUser.areaID;

                                InitAreas();
                                vm.disputeInformation.userID = currUser.id.toString();
                                vm.disputeInformation.uploadName = currUser.name;
                            });
                        }
                    });

                    function InitAreas() {
                        //绑定镇控件
                        areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                            vm.townList = result.data.items;
                            vm.disputeInformation.townID = currUser.townID.toString();
                        });
                        //绑定区控件
                        areaService.getAreaByLevelAsync({ id: townId }).then(function (result) {
                            vm.areaList = result.data.items;
                            vm.disputeInformation.areaID = currUser.areaID.toString();
                        });
                        //绑定村控件
                        areaService.getAreaByLevelAsync({ id: areaId }).then(function (result) {
                            vm.villageList = result.data.items;
                            vm.disputeInformation.villageID = currUser.villageID.toString();
                        });
                    }
                });
            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();